package com.efounder.fragment;


import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.efounder.activity.MallOrderDetailActivity;
import com.efounder.activity.ProductDetailsActivity;
import com.efounder.activity.ToEvaluateActivity;
import com.efounder.frame.baseui.BaseFragment;
import com.efounder.mobilemall.R;

import java.util.ArrayList;

/**
 * 评论fragment
 * @author 隗德彪
 */
public class AssessFragment extends BaseFragment {


    private String type;

    private RecyclerView mRecyclerView;

    private RecyclerView.Adapter mAdapter;

    private RecyclerView.LayoutManager mLayoutManager;


    public static AssessFragment newInstance() {
        AssessFragment fragment = new AssessFragment();
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_assess, container, false);
        type = this.getArguments().getString("type");
        initData();
        initView(view);
        return view;
    }

    private void initData() {
        mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mAdapter = new MyAdapter(getData());
    }

    private void initView(View view) {
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        // 设置布局管理器
        mRecyclerView.setLayoutManager(mLayoutManager);
        // 设置adapter
        mRecyclerView.setAdapter(mAdapter);
    }

    private ArrayList<String> getData() {
        ArrayList<String> data = new ArrayList<>();
        int size;
        if (type.equals("0")) {
            size = 5;
        } else {
            size = 2;
        }
        for (int i = 0; i < 0; i++) {
            data.add(i + "");
        }
        return data;
    }


    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

        private ArrayList<String> mData;

        public MyAdapter(ArrayList<String> data) {
            this.mData = data;
        }

        public void updateData(ArrayList<String> data) {
            this.mData = data;
            notifyDataSetChanged();
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            // 实例化展示的view
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_rv_item, parent, false);
            // 实例化viewholder
            ViewHolder viewHolder = new ViewHolder(v);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            // 绑定数据
//            holder.mTv.setText(mData.get(position));
            holder.assess.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getContext(), ToEvaluateActivity.class);
                    startActivity(intent);
                }
            });
            holder.seeDetail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getContext(), MallOrderDetailActivity.class);
                    intent.putExtra("orderBh", "18022717313910000000001296748823");//((OrderBody) orderList.get(position)).getOrderBh()
                    startActivity(intent);
                }
            });

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getContext(), ProductDetailsActivity.class);
                    intent.putExtra("goodbh", "001161350000017");
                    startActivity(intent);
                }
            });

            if("1".equals(type)){
                holder.assess.setText(R.string.mail_add_evaluation);
            }
        }

        @Override
        public int getItemCount() {
            return mData == null ? 0 : mData.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {

            TextView seeDetail;
            TextView assess;

            public ViewHolder(View itemView) {
                super(itemView);
                assess = itemView.findViewById(R.id.assess);
                seeDetail = (TextView) itemView.findViewById(R.id.seeDetail);
            }
        }
    }

}
